﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace C9MonthInReviewUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DateTime start = new DateTime(2014,7,1);
            DateTime end = new DateTime(2014,8,1);

            var str = new StringBuilder();

            str.Append(GetSummary("<h2>Coding4Fun Projects</h2>", "http://channel9.msdn.com/coding4fun/projects", start, end));
            str.Append(GetSummary("<h2>Coding4Fun Articles</h2>", "http://channel9.msdn.com/coding4fun/articles", start, end));
            str.Append(GetSummary("<h2>Coding4Fun Blog</h2>", "http://channel9.msdn.com/coding4fun/blog", start, end));
            str.Append(GetSummary("<h2>Coding4Fun Kinect Gallery</h2>", "http://channel9.msdn.com/coding4fun/kinect", start, end));
            
            html.Text = str.ToString();
            ClipboardHelper.CopyHtmlToClipBoard(str.ToString());
        }

        private string GetSummary(string title, string baseUrl, DateTime start, DateTime end)
        {
            var blogposts = Channel9WrapperLibrary.Posts.GetPosts(baseUrl, start, end);

            var blogpostsordered = from p in blogposts orderby p.PostDate select p;

            if (blogpostsordered.Count() == 0)
                return "";

            var str = new StringBuilder();
            str.AppendLine(title);

            foreach (var item in blogpostsordered)
            {
                str.Append("<a href=\"");
                str.Append(item.Url);
                str.Append("\" >");
                str.Append(item.Title);
                str.AppendLine("</a><br/>");
            }

            return str.ToString();
        }
    }
}
